package org.teremail.smtp;

import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.expect;

import java.util.HashMap;
import java.util.Map;

import org.apache.mina.common.IoSession;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
import org.easymock.IMocksControl;

import com.google.common.collect.Maps;

public class SMTPTestUtil {

    static IoSession createIoSession(boolean threadSafe) {
        return createIoSession(new HashMap<String, Object>(), threadSafe);
    }
    
    static IoSession createIoSession(final Map<String, Object> initialData, boolean threadSafe) {
        final Map<String,Object> m = Maps.newHashMap(initialData);
        
        IAnswer<Object> get = new IAnswer<Object>() {
            public Object answer() throws Throwable {
                String key = (String) EasyMock.getCurrentArguments()[0];
                return m.get(key);
            }
        };
        
        IAnswer<Object> set = new IAnswer<Object>() {
            public Object answer() throws Throwable {
                String key = (String) EasyMock.getCurrentArguments()[0];
                Object value = EasyMock.getCurrentArguments()[1];
                return m.put(key, value);
            }
        };
        
        IMocksControl mc = EasyMock.createNiceControl();
        mc.makeThreadSafe(threadSafe);
        
        //IoSession session = createNiceMock(IoSession.class);
        IoSession session = mc.createMock(IoSession.class);
        expect(session.getAttribute((String) anyObject())).andAnswer(get).anyTimes();
        expect(session.setAttribute((String) anyObject(), anyObject())).andAnswer(set).anyTimes();
        return session;
    }

}
